/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pipo
 */
public class HighScoreManager {
    
    private File saveFile;
    private List list;
    public HighScoreManager()
    {
        saveFile = new File(System.getProperty("user.dir"),  "highscores.hsf");
        this.list = new ArrayList<Score>();
    }
    private class Score implements Comparable<Score>
            {
                private Integer score;
                private String name;
                
                public Score(Integer score , String name)
                {
                    this.score = score;
                    this.name = name;
                }
                
                public int compareTo(Score s2)
                {
                    return this.score.compareTo(s2.score);
                }
                
                public String toString()
                {
                    String s = "      Player Name:     " +this.name +  "     Score:    " + this.score.toString() + "\n";
                    return s;
                }
        
            }
    
    
    public String readHighScores()
    {
        try
        {
            if(saveFile.exists())
            {
                FileReader fileReader = new FileReader(saveFile);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String nameAndScore = bufferedReader.readLine();
                while(nameAndScore != null)
                {
                    if(nameAndScore.length() < 1 || nameAndScore.equals("\n"))  
                        nameAndScore = bufferedReader.readLine();
                    else
                    {
                        String[] nandS;
                        nandS = nameAndScore.split(" ");
                        Integer scr  = Integer.parseInt(nandS[0]);
                        Score newScore = new Score(scr, nandS[1]);
                        list.add(newScore);   
                        nameAndScore = bufferedReader.readLine();

                    }
                }
                fileReader.close();
            }

        
        } catch (IOException e) {
			e.printStackTrace();
		}
        
        Collections.sort(list);
        Collections.reverse(list);
        String highS = "";
        for(int i = 0 ;  i < this.list.size() ; i++)
        {
            highS += (i+1)+ " .     " +  this.list.get(i).toString();
        }
        
        return highS;
             
    }
    
    public void addScore(int playerScore, String userName)
    {
        
        try {
            saveFile.createNewFile();
              
        } catch (IOException ex) {
            Logger.getLogger(SaveLoadManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(saveFile, true)))) {
            String append = "\r\n" + playerScore + " " + userName;
            out.print(append);
            out.close();
        }catch (IOException ex) {
           Logger.getLogger(SaveLoadManager.class.getName()).log(Level.SEVERE, null, ex);
 
    }
    
        
    
    }
    
    public void clearHighScores()
    {       
        saveFile.delete();
       
    }
}
